import numpy as np

# 混淆矩阵
cm = np.array(  [[446365, 229, 128, 72, 1596, 137, 11, 16, 1, 175, 3225, 66, 58],
    [644, 949, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [93, 0, 25139, 1, 125, 1, 0, 0, 0, 0, 0, 0, 0],
    [162, 0, 0, 1836, 3, 0, 2, 0, 0, 0, 0, 0,  51],
    [825, 0, 374, 2, 44425, 15, 0, 0, 0, 0, 0, 0, 1],
    [222, 0, 0, 0, 0, 814, 31, 0, 0, 0, 0, 0,  1],
    [340, 0, 0, 0, 0, 30, 758, 5, 0, 1, 32, 2,  1],
    [204, 0, 0, 0, 0, 0, 6, 1345, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1110, 0, 0, 0, 0],
    [230, 0, 0, 48, 0, 0, 0, 0, 0, 1476, 0, 0, 0],
    [2080, 0, 0, 0, 22, 0, 0, 0, 0, 0, 29672, 1, 1],
    [24, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 1163, 0],
    [174,0,0,1,1,0,0,0,0,0,0,476,2658]
])

labels = ["BENIGN", "Bot", "DDoS", "DoS GoldenEye", "DoS Hulk", "DoS Slowhttptest", "DoS slowloris",
          "FTP-Patator", "Heartbleed", "Infiltration", "PortScan", "SSH-Patator", "Web Attack"]

# 初始化指标列表
precisions = []
recalls = []
f1_scores = []
accuracies = []

# 计算每个类别的指标
for i in range(len(labels)):
    TP = cm[i, i]
    FP = np.sum(cm[:, i]) - TP
    FN = np.sum(cm[i, :]) - TP
    TN = np.sum(cm) - (TP + FP + FN)

    accuracy = (TP + TN) / np.sum(cm)
    precision = TP / (TP + FP) if (TP + FP) > 0 else 0
    recall = TP / (TP + FN) if (TP + FN) > 0 else 0
    f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0

    precisions.append(precision)
    recalls.append(recall)
    f1_scores.append(f1_score)
    accuracies.append(accuracy)

# 打印结果
for i, label in enumerate(labels):
    print(f"{label}:")
    print(f"  Accuracy: {accuracies[i]:.4f}")
    print(f"  Precision: {precisions[i]:.4f}")
    print(f"  Recall: {recalls[i]:.4f}")
    print(f"  F1 Score: {f1_scores[i]:.4f}")
    print()